﻿import odal.*;

class odal.OMSElement extends OMSNode {
	private var m_omsStructure:OMSStructure;
	private var m_parentElementId:String;
	private var m_schemeName:String;
	private var m_namespacePath:String;
	private var m_bookmark:String;
	private var m_Relations:Object;
	private var m_values:Object;

	public function OMSElement( omsStructure, id, elmname, bookmark, schemeName, namespacePath) {
		this.m_Relations = new Object();
		this.m_values = new Object();
		this.m_omsStructure = omsStructure;
		this.m_id = id;
		if ( elmname == null ){
			elmname = id;
		}//if
		this.m_name = elmname;
		this.m_bookmark = bookmark;
		this.m_schemeName = schemeName;
		this.m_namespacePath = namespacePath;
	}//OMSElements
	
	public function set ParentElementId( id:String ):Void {
		this.m_parentElementId = id;
	}//set ParentElementId

	public function get ParentElementId():String {
		return this.m_parentElementId;
	}//get ParentElementId 

	public function createValue( simpleAttributeName:String, datatypeEnumerationName:String, id:String, content:String ):Void {
		var value:OMSValue = new OMSValue( this, simpleAttributeName, datatypeEnumerationName, id, content );
		this.appendValue( value );
	}//createValue
	
	public function appendValue( value:OMSValue ):Void {
		var denValues:Object = this.m_values[ value.SimpleAttributeName ];
		if ( denValues == null ) {
			denValues = new Object();
		}//if
		denValues[ value.DatatypeEnumerationName ] = value;
//		trace( "[OMSElement.appendValue] value.SimpleAttributeName: " + value.SimpleAttributeName );
//		trace( "[OMSElement.appendValue] value.DatatypeEnumerationName: " + value.DatatypeEnumerationName );
		this.m_values[ value.SimpleAttributeName ] = denValues;
		//trace ( "this.m_values[" + value.SimpleAttributeName + "][" + value.DatatypeEnumerationName +"]" +this.m_values[ value.SimpleAttributeName ][ value.DatatypeEnumerationName ] );
	}//appendValue
	
	public function relate( targetElement:OMSElement, catName:String, catType:String, relId:String, key:String ):Void {
//		trace( '[OMSElement.relate] this.id: ' + this.Id + ' - this.Name: ' + this.Name );
		var relation:OMSRelation = null;
		relation = this.getRelationByComplexAttributeName( catName );
		if ( relation == null ) {
//			trace( '[OMSElement.relate] New relation!' );
			relation = new OMSRelation( this, targetElement, catName, catType, relId, key );
			this.m_Relations[ catName ] = relation;
		} else {
			relation.addElement2Relation( targetElement, relId, key );
		}//else
//		trace( '[OMSElement.relate] relation.TargetElements.length: ' + relation.TargetElements.length );
//		trace( '[OMSElement.relate] ******************************* ENDE ***********************************\n' );
	}//relate
	
	public function getJoinedElements( catName:String ):Array {
//		trace( '[OMSElement.getJoinedElements]' );
		var relation:OMSRelation = this.getRelationByComplexAttributeName( catName );
//		trace( '[OMSElement.getJoinedElements] relation: \n' + relation.toString() );
		return relation.TargetElements;
//		return new Array();
	}//getJoinedElements;
	
	public function get SchemeName():String {
		return this.m_schemeName;
	}//get SchemeName

	public function get NamespacePath():String {
		return this.m_namespacePath;
	}//get NamespacePath

	public function get Bookmark():String {
		return this.m_bookmark;
	}//get Bookmark
	
	public function getValue( simpleAttributeName:String, datatypeEnumerationName:String ):String {
		var value:String = "";
		var omsValue:OMSValue = this.getValueObject( simpleAttributeName, datatypeEnumerationName );
		if ( omsValue != null ){
			value = omsValue.Content;
//			trace( "[OMSElement.getValue] omsValue.Content: " + omsValue.Content );
//			trace( "[OMSElement.getValue] value: " + value );
		}//if
		return value;
	}//getValue
	
	public function getValueObject( simpleAttributeName:String, datatypeEnumerationName:String ):OMSValue {
		return this.m_values[ simpleAttributeName ][ datatypeEnumerationName ];
	}//function
	
	public function getRelationByComplexAttributeName( p_catName:String ):OMSRelation {
		var relation:OMSRelation = OMSRelation( this.getObjectFromCollectionById( p_catName, this.m_Relations ) );
//		trace( '[OMSElement.getRelationByComplexAttributeName] relation.SourceElement.Id: ' + relation.SourceElement.Id );		
		return relation;
	}//getRelationByComplexAttributeName
	
}//odal.OMSElement